﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XRPGLibrary;
using XRPGLibrary.TileEngine;
using XRPGLibrary.SpriteClasses;
using System.IO;
//using System.Text;

namespace myRPG.GameScreens
{
    public class towerScreen : BaseGameState
    {
        #region Field Region
        Engine engine = new Engine(32, 32);
        Tileset tileset;
        TileMap map;
        Player player;
        AnimationSprite Princess;
        #endregion
        #region Property Region
        #endregion
        #region Constructor Region
        public towerScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
            player = new Player(game);
        }
        #endregion #region XNA Method Region
        public override void Initialize()
        {
            base.Initialize();
        }
        protected override void LoadContent()
        {

            Texture2D spriteSheet = Game.Content.Load<Texture2D>(@"img\playerSprite");
            Dictionary<AnimationKey, Animation> animations = new Dictionary<AnimationKey, Animation>();
            Animation animation = new Animation(8, 45, 55, 0, 0);
            animations.Add(AnimationKey.Down, animation);
            animation = new Animation(8, 45, 55, 0, 55);
            animations.Add(AnimationKey.Up, animation);
            animation = new Animation(8, 45, 55, 0, 110);
            animations.Add(AnimationKey.Left, animation);
            animation = new Animation(8, 45, 55, 0, 165);
            animations.Add(AnimationKey.Right, animation);
            Princess = new AnimationSprite(spriteSheet, animations);

            string content = System.IO.File.ReadAllText(@"../../../data/tower.TXT");
            string[] contentArray = content.Split(new char[] { ',', '{', '}', '\r', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            int[] array = new int[2500];
            for (int i = 0; i < contentArray.Length; i++)
            {

                array[i] = Convert.ToInt32(contentArray[i]) - 1;
            }

            Texture2D tilesetTexture = Game.Content.Load<Texture2D>(@"Backgrounds\towerTiles");
            tileset = new Tileset(tilesetTexture, 10, 19, 64, 64);

            MapLayer layer = new MapLayer(25, 25);
            for (int y = 0; y < layer.Height; y++)
            {
                for (int x = 0; x < layer.Width; x++)
                {
                    Tile tile = new Tile(array[x + y * 25], 0);
                    layer.SetTile(x, y, tile);
                }
            }
            map = new TileMap(tileset, layer);
            //  MapLayer splatter = new MapLayer(50, 50);
            /*Random random = new Random();
            //在40*40的图层上添加80个tile对象
            for (int i = 0; i < 80; i++)
            {
                int x = random.Next(0, 40);
                int y = random.Next(0, 40);
                //在2到14这些图片中随机选择一个
                int index = random.Next(2, 14);
                //int index = 2;
                Tile tile = new Tile(index, 0);//利用tile索引来初始化tile对象
                splatter.SetTile(x, y, tile);//往新的图层上添加tile对象
            }
            map.AddLayer(splatter);
             */
            //绘制地图
            base.LoadContent();
        }
        public override void Update(GameTime gameTime)
        {
            player.Update(gameTime);
            Princess.Update(gameTime);
            Vector2 motion = new Vector2();//方向向量
            //按W键，精灵向上移动
            if (InputHandler.KeyDown(Keys.W))
            {
                Princess.CurrentAnimation = AnimationKey.Up;
                motion.Y = -1;
            }
            //按S键，精灵向下移动
            else if (InputHandler.KeyDown(Keys.S))
            {
                Princess.CurrentAnimation = AnimationKey.Down;
                motion.Y = 1;
            }
            if (InputHandler.KeyDown(Keys.A))
            {
                Princess.CurrentAnimation = AnimationKey.Left;
                motion.X = -1;
            }
            else if (InputHandler.KeyDown(Keys.D))
            {
                Princess.CurrentAnimation = AnimationKey.Right;
                motion.X = 1;
            }
            if (motion != Vector2.Zero)
            {
                Princess.IsAnimating = true;//精灵发生移动，在Update方法中需要更新
                motion.Normalize();
                Princess.Position += motion * Princess.Speed;//单位化方向向量，并乘以速度，重置精灵位置
                Princess.LockToMap();//控制精灵的位置，防止移出地图
                if (player.Camera.mode == CameraMode.Follow)//如果当前Camera的状态是Follow，则将精灵和Camera关联起来，Camera的位置会跟着sprite的位置移动
                    player.Camera.LockToSprite(Princess);
            }
            else
            {
                Princess.IsAnimating = false;
            }
            //按F键，实现Camera状态的切换
            if (InputHandler.KeyReleased(Keys.F))
            {
                player.Camera.ToggleCameraMode();
                //如果切换后的Camera状态变为Follow，将其与sprite关联
                if (player.Camera.mode == CameraMode.Follow)
                    player.Camera.LockToSprite(Princess);
            }
            if (player.Camera.mode != CameraMode.Follow)
            {
                if (InputHandler.KeyReleased(Keys.C))
                {
                    player.Camera.LockToSprite(Princess);
                }
            }
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin(
            SpriteSortMode.Immediate,
            BlendState.AlphaBlend,
            SamplerState.PointClamp,
            null,
            null,
            null,
            Matrix.Identity);
            map.Draw(GameRef.SpriteBatch, player.Camera);//绘制地图
            Princess.Draw(gameTime, GameRef.SpriteBatch, player.Camera);
            base.Draw(gameTime);
            GameRef.SpriteBatch.End();
        }
        #region Abstract Method Region
        #endregion
    }
}